%% @author wenqi_zheng
%% @doc @todo Add description to simple_web_server.


-module(simple_web_server).

%% ====================================================================
%% API functions
%% ====================================================================
-export([init/3, handle/2, terminate/3]).


init({tcp, http}, Req, _Opts) ->
	{ok, Req, undefined}.


handle(Req, State) ->
	{Path, Req1} = cowboy_req:path(Req),
	Response = read_file1(Path),
	{ok, Req2} = cowboy_req:reply(200, [], Response, Req1),
	{ok, Req2, State}.


terminate(_Reason, _Req, _State) ->
	ok.
%% ====================================================================
%% Internal functions
%% ====================================================================

read_file1(Path) ->
	File = ["."|binary_to_list(Path)],
	case file:read_file(File) of
		{ok, Bin} -> Bin;
		_ -> ["<pre>cannot read:", File, "</pre>"]
	end.
